const requestFile = require("../../utils/request.js");
const request = requestFile.request;

const routerFile = require("../../utils/router.js");
const router = routerFile.router;

const timeUtilFile = require("../../utils/timeUtil.js");
const timeUtil = timeUtilFile.timeUtil;

const constantFile = require("../../utils/constant.js")
const constant = constantFile.constant;

Page({
  data: {

    initialized: false,

    swiper: {
      showDots: true,
      defaultColor: 'rgba(0, 0, 0, .3)',
      activeColor: "#000000",
      autoplay: true,
      interval: 5000,
      duration: 5000,
      circular: true,
      swiperPic: router.indexSwiperImgs,
    },

    items: [],
    pageNum: 0,
    pageSize: 10,

  },

  onShow: function(e) {
    const isLogin = wx.getStorageSync('isLogin');
    if (isLogin == null || isLogin == undefined || isLogin == false) { // 用户未登录
      wx.navigateTo({
        url: '../login/login',
      })
      return;
    } else {
      this.refresh();
    }
  },

  // 页面触底事件，追加item项
  onReachBottom: function(e) {
    var that = this;
    var currentPageNum = this.data.pageNum;
    const nextPageNum = currentPageNum + 1;
    const pageSize = this.data.pageSize;
    request(router.getItems, "GET", {
      pageNum: nextPageNum,
      pageSize: pageSize,
    }).then(function(res) {
      const items = res.data.data.items;
      if (items.length > 0) {
        for (var i = 0; i < items.length; i++) {
          items[i].need = 0;
        }
        var currentItems = that.data.items;
        // 将获取到的items项追加到当前的items项中
        currentItems = currentItems.concat(items);
        that.setData({
          items: currentItems,
          pageNum: nextPageNum,
        })
      }
    })
  },

  // 当需求数量变化时，设置页面数据变化
  onNeedChanged: function(e) {
    const {index, need} = e.detail;
    this.data.items[index].need = need;
  },

  // 重置选择
  handleReset: function(e) {
    var items = this.data.items;
    for (var i = 0; i < items.length; i++) {
      items[i].need = 0;
    }
    this.setData({
      items: items,
    })
  },

  // 提交申请
  handleSubmit: function(e) {

    // 请求后端服务器生成订单
    var that = this;
    const items = that.data.items;
    var requestItems = [];
    for (var i = 0; i < items.length; i++) {
      if (items[i].need != undefined && items[i].need != null && items[i].need != 0) {
        var requestItem = {
          itemid: items[i].id,
          name: items[i].name,
          number: items[i].need,
        }
        requestItems.push(requestItem);
      }
    }
    var paramStr = "";
    for (var i = 0; i < requestItems.length; i++) {
      paramStr = paramStr + requestItems[i].name + " * " + requestItems[i].number + "; ";
    }

    if (requestItems.length > 0) {
      wx.navigateTo({
        url: 'generate_order/generate_order?items='+paramStr,
        events: {
          onTimeSelected: function(data) {
            var useDate = data.useDate;
            var returnDate = data.returnDate;
            const userModal = wx.getStorageSync('user');
            for (var i = 0; i < requestItems.length; i++) {
              requestItems[i].name = 0;
            }
            const requestItemsStr = JSON.stringify(requestItems);
            request(router.generateOrder, "POST", {
              items: requestItemsStr,
              userModal: userModal,
              useDate: useDate,
              returnDate: returnDate,
            }).then(function(res) {
              if (res.data.code == 200) {
                const order = res.data.data.order;
                const applyDate = order.applyDate;
                const applyAccount = order.applyUser.account;
                const applyUser = order.applyUser.description;
                const applyDept = order.dept.name;
                const applyItems = JSON.stringify(order.items);
                const orderId = order.orderId;
                const returnDate = order.returnDate;
                const useDate = order.useDate;
                const status = order.status;
                const navigateUrl = "../../pages/order/order?applyDate=" + applyDate + "&applyAccount=" + applyAccount + "&applyUser=" + applyUser + "&applyDept=" + applyDept + "&applyItems=" + applyItems + "&orderId=" + orderId + "&returnDate=" + returnDate + "&useDate=" + useDate + "&status=" + status;
                wx.navigateTo({
                  url: navigateUrl,
                })
              }
            })
          }
        }
      })
    }
  },

  onPullDownRefresh: function() {
    this.refresh();
  },

  refresh : function() {
    const that = this;
    request(router.getItems, "GET", {
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
    }).then (function(res) {
      var items = res.data.data.items;
      for (var i = 0; i < items.length; i++) {
        items[i].need = 0;
      }
      that.setData({
        items: items,
        initialized: true,
      })
    })
  }

})
